package com.ocr.fragment;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import com.ocr.MainActivity;
import com.ocr.R;
import com.ocr.R.layout;
import com.ocr.adapter.HistoryAdapter;
import com.ocr.entities.HistoryItem;
import com.ocr.utils.ApplicationConstant;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;

public class FragmentHistory extends Fragment {
	private List<HistoryItem> listHistoryItems = new ArrayList<HistoryItem>();
	private ListView listViewHistory;
	private HistoryAdapter adapter;
	private MainActivity mainActivity;
	private LinearLayout noHistoryLayout;
	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		this.mainActivity = (MainActivity) activity;
	}

	private void deleteAllHistory() {
		listHistoryItems.clear();
		MainActivity.HISTORYDAO.deleteAll();
		adapter.notifyDataSetChanged();
		showHideStatusLayout();
	}
	public void showHideStatusLayout(){
		//show hide layout nohistory
				if(listHistoryItems == null || listHistoryItems.size() ==0)
				{
					noHistoryLayout.setVisibility(View.VISIBLE);
					listViewHistory.setVisibility(View.GONE);
				}
				else
				{
					noHistoryLayout.setVisibility(View.GONE);
					listViewHistory.setVisibility(View.VISIBLE);
				}
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		setHasOptionsMenu(true);
		View root = inflater.inflate(R.layout.fragment_history, container,
				false);
		noHistoryLayout = (LinearLayout) root.findViewById(R.id.layout_no_history);
		listViewHistory = (ListView) root.findViewById(R.id.listView_history);
		listHistoryItems = MainActivity.HISTORYDAO.loadAll();
		showHideStatusLayout();
		adapter = new HistoryAdapter(getActivity(), R.layout.item_history,
				listHistoryItems);
		listViewHistory.setAdapter(adapter);
		Log.d(ApplicationConstant.LOGT_TAG, "--list history size:"
				+ listHistoryItems.size());
		Collections.sort(listHistoryItems);
		for (HistoryItem historyItem : listHistoryItems) {
			System.out.println(historyItem);
		}
		// action
		listViewHistory.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				HistoryItem historyItem = listHistoryItems.get(arg2);
				mainActivity.navigateToFragmentResult(historyItem
						.getOCRResult());
			}
		});
		return root;
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		inflater.inflate(R.menu.main, menu);
		super.onCreateOptionsMenu(menu, inflater);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case R.id.action_history_delete_all:
			deleteAllHistory();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
}
